Page({
    data: {
      userInfo: null
    },
  
    onLoad: function() {
      this.loadUserInfo();
    },
  
    onShow: function() {
      this.loadUserInfo();
    },
  
    loadUserInfo: function() {
      const that = this;
      wx.request({
        url: `${getApp().globalData.baseUrl}/api/user/info`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        },
        success(res) {
          if (res.data.success) {
            that.setData({
              userInfo: res.data.userInfo
            });
          }
        }
      });
    },
  
    navigateToClassManage: function() {
      wx.switchTab({
        url: '/pages/classes/classes'
      });
    },
  
    navigateToScheduleManage: function() {
      wx.switchTab({
        url: '/pages/schedule/schedule'
      });
    },
  
    navigateToSettings: function() {
      wx.navigateTo({
        url: '/pages/settings/settings'
      });
    },
  
    showClassCode: function() {
      wx.request({
        url: `${getApp().globalData.baseUrl}/api/user/class-code`,
        method: 'GET',
        header: {
          'Authorization': `Bearer ${wx.getStorageSync('token')}`
        },
        success(res) {
          if (res.data.success) {
            wx.showModal({
              title: '班级码',
              content: res.data.classCode,
              showCancel: false
            });
          }
        }
      });
    },
  
    handleLogout: function() {
      wx.showModal({
        title: '提示',
        content: '确定要退出登录吗？',
        success(res) {
          if (res.confirm) {
            // 清除本地存储的登录信息
            wx.clearStorageSync();
            // 跳转到登录页
            wx.reLaunch({
              url: '/pages/auth/auth'
            });
          }
        }
      });
    }
  });